package com.letv.core.parser;

import com.letv.core.bean.VideoFileBean;
import com.letv.core.bean.VideoFileBean.VideoSchedulingAddress;
import com.letv.core.utils.LogInfo;

import org.json.JSONObject;

/**
 * 视频文件体解析
 *
 * @author lihui3
 * */
public class VideoFileParser extends VideoSchedulingAddressParser {

    public VideoFileParser(boolean needPay) {
        super(needPay);
    }

    private final String VIDEOFILE = "videofile";

    private final String INFOS = "infos";

    private final String MP4_350 = "mp4_350";

    private final String MP4_1000 = "mp4_1000";

    private final String MP4_1300 = "mp4_1300";

    // add dolby type

    private final String MP4_800_DB = "mp4_800_db";

    private final String MP4_1300_DB = "mp4_1300_db";

    private final String MP4_720P_DB = "mp4_720p_db";

    private final String MP4_1080P6M_DB = "mp4_1080p6m_db";

    private final String MMSID = "mmsid";

    @Override
    protected VideoSchedulingAddress parse(JSONObject data) throws Exception {
        return super.parse(data);
    }

    public VideoFileBean parseVideoFile(JSONObject data) throws Exception {
        if (data.has("header")) {
            JSONObject header = getJSONObject(data, "header");

            if (header != null) {
                if (header.has("status")) {
                    int status = getInt(header, "status");
                    if (status == 5) {
                        LogInfo.log("VideoFileParser", "parseVideoFile status == 5");
                        VideoFileBean fileBean = new VideoFileBean();
                        fileBean.setErr(true);
                        return fileBean;
                    }
                }
            }
        }

        VideoFileBean videoFile = null;

        data = getJSONObject(data, "body");
        data = getJSONObject(data, VIDEOFILE);

        videoFile = new VideoFileBean();

        videoFile.setMmsid(getString(data, MMSID));

        data = getJSONObject(data, INFOS);
        if (has(data, MP4_350)) {
            videoFile.setMp4_350(parse(getJSONObject(data, MP4_350)));
        }

        if (has(data, MP4_1000)) {
            videoFile.setMp4_1000(parse(getJSONObject(data, MP4_1000)));
        }

        if (has(data, MP4_1300)) {
            videoFile.setMp4_1300(parse(getJSONObject(data, MP4_1300)));
        }

        if (has(data, MP4_800_DB)) {
            videoFile.setMp4_800_db(parse(getJSONObject(data, MP4_800_DB)));
        }

        if (has(data, MP4_1300_DB)) {
            videoFile.setMp4_1300_db(parse(getJSONObject(data, MP4_1300_DB)));
        }

        if (has(data, MP4_720P_DB)) {
            videoFile.setMp4_720p_db(parse(getJSONObject(data, MP4_720P_DB)));
        }

        if (has(data, MP4_1080P6M_DB)) {
            videoFile.setMp4_1080p6m_db(parse(getJSONObject(data, MP4_1080P6M_DB)));
        }

        return videoFile;
    }

}
